package com.devgroupvn.dota.data;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GetHero {
	
	public ArrayList<Hero> herolist ;
	private final String ENCODE_UTF_8 = "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></html>";
	public GetHero() {
		herolist = new ArrayList<Hero>();
	}
	
	public List<Hero> getListHeroes() {
		try {
			String url = "http://www.joindota.com/en/content/2553";
			
			Response res = Jsoup.connect(url).header("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2").userAgent("Mozilla").execute();
	        if(res== null)
	        {
	        	return null;
	        }
			Document doc = res.parse();
			
			Elements list = doc.select(".heroset");
			
			for (Element heroset : list) {
				
				
				String hrh1 = heroset.select(".hrhl").first().text();
				
				Elements heroes = heroset.select(".herodude");
				
				for (Element item : heroes) {
					Hero hero = new Hero();
					
					hero.image = item.select("img").first().attr("src");
					hero.name = item.select("span").first().text();
					hero.link = item.select("a").first().attr("href");
					
					hero.set = hrh1;
					
					herolist.add(hero);
				}

			}
			return herolist;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Hero getHeroDetail(Hero hero) {
//		Hero hero = new Hero();
		String url = hero.link;
		try {
			Response res = Jsoup.connect(url).header("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2").userAgent("Mozilla").execute();
	        if(res ==null)
	        {
	        	return null;
	        }
			Document doc = res.parse();
			
			hero.headline = doc.select(".headline").first().text();
			hero.info = ENCODE_UTF_8+ doc.select(".hero1").first().html();
			hero.abilities =ENCODE_UTF_8+ doc.select(".hero2").first().html();
			hero.description = doc.select(".lore").first().text();
			hero.spec = ENCODE_UTF_8+ doc.select(".herospec").first().html();
			hero.youtubelink = doc.select(".heromedia").first().select("a").attr("href");
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return hero;
	}
}
